<script setup>
import { usePermissionStore } from '@/stores/permission'
import { ref, onMounted} from 'vue'

const permissionStore = usePermissionStore()
const showAddRoleForm = ref(false)
const newRoleName = ref('')
const newRoleDesc = ref('')

const loading = ref(false)
const error = ref(null)

</script>

<template>
  <div class="py-6">
    <div class="mb-6">
      <h1 class="text-2xl font-bold text-gray-900">权限管理</h1>
      <p class="mt-1 text-sm text-gray-500">管理系统中的角色和权限</p>
      <div v-if="error" class="mt-4 p-4 bg-red-50 border-l-4 border-red-400">
        <div class="flex">
          <div class="flex-shrink-0">
            <svg class="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor">
              <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" />
            </svg>
          </div>
          <div class="ml-3">
            <p class="text-sm text-red-700">{{ error }}</p>
          </div>
        </div>
      </div>
      <div v-if="loading" class="mt-4 p-4 bg-blue-50 border-l-4 border-blue-400">
        <div class="flex items-center">
          <svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-blue-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
            <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
            <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
          </svg>
          <span class="text-sm text-blue-700">加载中...</span>
        </div>
      </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
      <!-- 角色列表 -->
      <div class="bg-white shadow rounded-lg">
        <div class="px-4 py-5 sm:px-6 border-b border-gray-200">
          <div class="flex items-center justify-between">
            <h3 class="text-lg font-medium leading-6 text-gray-900">角色列表</h3>
            <button
              @click="showAddRoleForm = true"
              class="inline-flex items-center px-3 py-1.5 border border-transparent text-xs font-medium rounded shadow-sm text-white bg-primary hover:bg-primary-dark focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary">
              <i class="fa fa-plus mr-1"></i>添加角色
            </button>
          </div>
          <div v-if="showAddRoleForm" class="mt-4">
            <input
              v-model="newRoleName"
              placeholder="角色名称"
              class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary focus:border-primary"
            >
            <input
              v-model="newRoleDesc"
              placeholder="角色描述"
              class="w-full mt-2 px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary focus:border-primary"
            >
            <div class="mt-2 flex space-x-2">
              <button
                @click="addRole"
                class="flex-1 inline-flex items-center px-3 py-1.5 border border-transparent text-xs font-medium rounded shadow-sm text-white bg-primary hover:bg-primary-dark focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary">
                确认
              </button>
              <button
                @click="showAddRoleForm = false"
                class="flex-1 inline-flex items-center px-3 py-1.5 border border-gray-300 text-xs font-medium rounded shadow-sm text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary">
                取消
              </button>
            </div>
          </div>
        </div>
        <div class="divide-y divide-gray-200">
          <div
            v-for="role in permissionStore.roles"
            :key="role.id"
            class="px-4 py-4 hover:bg-gray-50 cursor-pointer"
            @click="selectRole(role)"
          >
            <div class="flex items-center justify-between">
              <div>
                <p class="text-sm font-medium text-gray-900">{{ role.name }}</p>
                <p class="text-sm text-gray-500">{{ role.description }}</p>
              </div>
              <span
                @click.stop="toggleRoleStatus(role)"
                class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full"
                :class="permissionStore.roleStatusClass(role.status)"
              >
                {{ role.status === 1 ? '启用' : '禁用' }}
              </span>
            </div>
          </div>
        </div>
      </div>

      <!-- 权限列表和分配 -->
      <div class="lg:col-span-2 bg-white shadow rounded-lg">
        <div class="px-4 py-5 sm:px-6 border-b border-gray-200">
          <h3 class="text-lg font-medium leading-6 text-gray-900">权限分配</h3>
        </div>
        <div class="p-4">
          <div class="mb-4">
            <h4 class="text-md font-medium text-gray-700 mb-2">当前角色:
              <span class="text-primary">{{ permissionStore.selectedRole?.name || '未选择角色' }}</span>
            </h4>
          </div>

          <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div
              v-for="permission in permissionStore.permissions"
              :key="permission.id"
              class="flex items-start"
            >
              <div class="flex items-center h-5">
                <input
                  :id="`permission-${permission.id}`"
                  type="checkbox"
                  :checked="permissionStore.isPermissionAssigned(permission.id)"
                  class="focus:ring-primary h-4 w-4 text-primary border-gray-300 rounded"
                >
              </div>
              <div class="ml-3 text-sm">
                <label
                  :for="`permission-${permission.id}`"
                  class="font-medium text-gray-700"
                >
                  {{ permission.name }}
                </label>
                <p class="text-gray-500">{{ permission.code }}</p>
              </div>
            </div>
          </div>

          <div class="mt-6 flex justify-end">
            <button
              @click="savePermissions"
              :disabled="!permissionStore.selectedRole"
              class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-primary hover:bg-primary-dark focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary disabled:bg-gray-400 disabled:cursor-not-allowed"
            >
              保存更改
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
